package com.motu.monstercity.server.game.timedtask;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.logic.FeastManager;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.monstercity.server.game.userdata.FeastChatRecord;
import com.motu.monstercity.server.game.userdata.ServerFeast;
import com.motu.monstercity.server.game.userdata.ServerSnatchOffer;
import com.motu.monstercity.server.game.userdata.UserFeastRecord;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.threadpool.DBLogManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class ServerSnatchOfferTask implements Runnable, Delayed {
	public final static Logger logger = LoggerFactory.getLogger(ServerSnatchOfferTask.class);

	private int serverId;// 服务器id
	private long userId;// 被悬赏的userid
	private long execTime;// 结束时间,任务的执行时间 悬赏时间+有效期12小时
	private JsonObject jsonObject;

	private int getServerId() {
		return this.serverId;
	}
	private long getUserId() {
		return this.userId;
	}
	private long getExecTime() {
		return this.execTime;
	}
	private JsonObject getJsonObject() {
		return this.jsonObject;
	}


	public ServerSnatchOfferTask(JsonObject obj) {
		this.serverId = obj.getInteger("server_id");
		this.userId = obj.getLong("user_id");
		this.execTime = obj.getLong("end_time");
		this.jsonObject = obj;
	}

	@Override
	public void run() {
		TimedTaskManager.sendWorkTaskRetry(TimeTaskCmd.WORK_SERVER_SNATCH_OFFER, getJsonObject());// 发送失败进行重发
	}

	@Override
	public long getDelay(TimeUnit unit) {
		return unit.convert(getExecTime() - TimeUtils.getCurTime(), TimeUnit.SECONDS);
	}

	@Override
	public int compareTo(Delayed o) {
		ServerSnatchOfferTask that = (ServerSnatchOfferTask) o;
		return getExecTime() > that.execTime ? 1 : (getExecTime() < that.execTime ? -1 : 0);
	}

	/**
	 *  处理悬赏时间结束的逻辑
	 * @param task
	 */
	public static void runTask(JsonObject task){
		try {
			logger.info("### ServerSnatchOfferTask runTask = " + task);
			long userId = task.getLong("user_id");// 被悬赏的userid
			int serverId = task.getInteger("server_id");
			long endTime = task.getLong("end_time");
			ServerSnatchOffer serverSnatchOffer = FightManager.getServerSnatchOffer(serverId, userId);
			// 悬赏时间正确，表示悬赏未提前结束
			if (serverSnatchOffer != null && serverSnatchOffer.getIsSendReward() == 0 && serverSnatchOffer.getPublishTime() + AllParam.SNATCH_OFFER_DURATION_TIME == endTime) {
				serverSnatchOffer.sendCompleteReward();
				serverSnatchOffer.update();
			}
		} catch (Exception e){
			String errMsg = Tool.getException(e);
			logger.error("###ServerSnatchOfferTask error:" + errMsg);
			DBLogManager.addServerCrashLog(999999, errMsg);
		}
	}
}
